#include "mrr.h"
#include "rectype.h"
#include "util.h"
#include "cnfield.h"
#include "nmfield.h"


using namespace std;
using namespace opsrc::stdf;

Mrr::Mrr()
{
}

Mrr::Mrr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf )
    :RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex), en(e){
    if (buf != 0)
        readFields(buf);
}


Mrr::~Mrr() {
}

void Mrr::setFinishTime(StdfField<long> tm) {
    finish_t = tm;
}

const StdfField<long> Mrr::getFinishTime() const {
    return finish_t;
}

void Mrr::setDispCode(StdfField<string> dispcod) {
    disp_cod = dispcod;
}

const StdfField<string> Mrr::getDispCode() const {
    return disp_cod;
}

void Mrr::setUserDescription(StdfField<string> usrdesc) {
    usr_desc = usrdesc;
}

const StdfField<string> Mrr::getUserDescription() const {
    return usr_desc;
}

void Mrr::setExecDescription(StdfField<string> execdesc) {
    exc_desc = execdesc;
}

const StdfField<string> Mrr::getExecDescription() const {
    return exc_desc;
}

void Mrr::readFields(char *buf) {

#if _DEBUG
    cout << "stdfrec= MRR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    // read finish time of last part
    int curpos = 0;
    NmField<unsigned long, 4> fint(buf+curpos, en);
    finish_t.set(fint.getData(), curpos, fint.getByteCount(), fint.getTotalByteCount());    
    curpos += finish_t.getTotalByteCount();

    // read disposition code, only 1 char
    if (curpos < reclen -1) {
        CnField<string, 1> dispcod(buf + curpos, en);
        disp_cod.set(dispcod.getData(), curpos, dispcod.getByteCount(), dispcod.getTotalByteCount());
        curpos += disp_cod.getTotalByteCount();
    }

    // Read user lot description 
    if (curpos < reclen -1) {
        CnField<string> usrdesc(buf + curpos, en);
        usr_desc.set(usrdesc.getData(), curpos, usrdesc.getByteCount(), usrdesc.getTotalByteCount());
        curpos += usr_desc.getTotalByteCount();
    }

    // Read execution code lot description
    if (curpos < reclen -1) {
        CnField<string> excdesc(buf + curpos, en);
        exc_desc.set(excdesc.getData(), curpos, excdesc.getByteCount(), excdesc.getTotalByteCount());
    }
}
